package com.vercon.moensmart.fragment.manager

import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.vercon.moensmart.R
import com.vercon.moensmart.dialog.LoginDialog
import com.vercon.moensmart.fragment.AppFragment
import com.vercon.moensmart.fragment.HomeFragment

class FragmentOpenManager {

    /**
     * Fragment 管理器
     */
    private var fManager: FragmentManager? = null

    /**
     * 装Fragment的容器
     */
    private val layoutId = 0

    companion object{
        private var fragmentOpenManager: FragmentOpenManager? = null
        fun getInstance(): FragmentOpenManager {
            if(fragmentOpenManager == null){
                fragmentOpenManager = FragmentOpenManager()
            }
            return fragmentOpenManager as FragmentOpenManager
        }
    }

    /**
     * 该Activity所有fragment的集合
     */
    private val fragments: ArrayList<Fragment> = ArrayList()


    private val tag = FragmentOpenManager::class.java.simpleName

    fun removeAll(activity: FragmentActivity){
        try {
            if(fragments.size > 0){
                for ( (i,fragmentItem) in fragments.withIndex() ){
                    closeFragment(activity)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun addFragment(activity: FragmentActivity, fragment: Fragment) {
        try {
            fragments.add(fragment)
            fManager = activity.supportFragmentManager
            val fragmentTransaction = fManager!!.beginTransaction()
            fragmentTransaction.add(R.id.fragmentContainerView, fragment, fragment.javaClass.simpleName).commitAllowingStateLoss()
            fragment.setMenuVisibility(true)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun goToLoginFragment(activity: FragmentActivity) {
        try {
            addFragment(activity,LoginDialog())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun goToLoginFragment2(activity: FragmentActivity) {
        try {
            for ( (i,fragmentItem) in fragments.withIndex() ){
                fManager = activity.supportFragmentManager
                val fragmentTransaction = fManager!!.beginTransaction()
                fragmentTransaction.remove(fragmentItem).commitAllowingStateLoss()
                fragments.removeAt(i)
            }
            addFragment(activity,LoginDialog())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun goToHomeFragment(activity: FragmentActivity) {
        addFragment(activity, HomeFragment())
        addAndHideFragment(activity,AppFragment())
    }

//    fun replaceFragment(activity: FragmentActivity, fragment: Fragment) {
//        try {
//            for ( (i,fragmentItem) in fragments.withIndex() ){
//                if(fragmentItem.javaClass.simpleName.equals(fragment.javaClass.simpleName) && fragmentItem.isAdded){
//                    return
//                }
//            }
//            fragments.add(fragment)
//            fManager = activity.supportFragmentManager
//            val fragmentTransaction = fManager!!.beginTransaction()
//            fragmentTransaction.replace(R.id.fragmentContainerView, fragment, fragment.javaClass.simpleName).commitAllowingStateLoss()
//            fragment.setMenuVisibility(true)
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//    }


    fun addAndHideFragment(activity: FragmentActivity, fragment: Fragment) {
        try {
            fragments.add(fragment)
            fManager = activity.supportFragmentManager
            val fragmentTransaction = fManager!!.beginTransaction()
            fragmentTransaction.add(R.id.fragmentContainerView, fragment, fragment.javaClass.simpleName)
            fragmentTransaction.hide(fragment).commitAllowingStateLoss()
            fragment.setMenuVisibility(false)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun hideFragment(activity: FragmentActivity, fragmentName: String) {
        try {
            for ( (i,fragmentItem) in fragments.withIndex() ){
                if(fragmentItem.javaClass.simpleName.equals(fragmentName)){
                    fManager = activity.supportFragmentManager
                    val fragmentTransaction = fManager!!.beginTransaction()
                    fragmentItem.setMenuVisibility(false)
                    fragmentTransaction.hide(fragmentItem).commitAllowingStateLoss()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun showFragment(activity: FragmentActivity, fragment: Fragment) {
        try {
            for ( (i,fragmentItem) in fragments.withIndex() ){
                if(fragmentItem.javaClass.simpleName.equals(fragment.javaClass.simpleName)){
                    fManager = activity.supportFragmentManager
                    val fragmentTransaction = fManager!!.beginTransaction()
                    fragmentItem.setMenuVisibility(true)
                    fragmentTransaction.show(fragmentItem).commitAllowingStateLoss()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            if(fragment.javaClass.simpleName.equals(AppFragment.javaClass.simpleName)){
                for ( (i,fragmentItem) in fragments.withIndex() ){
                    if(fragmentItem.javaClass.simpleName.equals(fragment.javaClass.simpleName)){
                        fManager = activity.supportFragmentManager
                        val fragmentTransaction = fManager!!.beginTransaction()
                        fragmentTransaction.remove(fragmentItem).commitAllowingStateLoss()
                    }
                }
                addAndHideFragment(activity,AppFragment())
            }

        }
    }

    fun closeFragment(activity: FragmentActivity) {

        try {
            Log.e("FragmentOpenManager","closeFragment fragments name "  + fragments[fragments.size - 1].javaClass.simpleName);
            fManager = activity.supportFragmentManager
            val fragmentTransaction = fManager!!.beginTransaction()
            fragmentTransaction.remove(fragments[fragments.size - 1]).commitAllowingStateLoss()
            fragments.removeAt(fragments.size - 1)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    fun clear(){
        fragmentOpenManager = null
    }
}